package cn.lbd.pseudoasynchronous;

import java.util.concurrent.*;

/**
 * 使用线程池和任务队列实现伪异步IO，优化BIO
 */
public class HandlerExecutorPool {
    private ExecutorService executor;

    public HandlerExecutorPool(int maxSizePool,int queueSize){
        this.executor = new ThreadPoolExecutor(
                Runtime.getRuntime().availableProcessors(),
                maxSizePool,
                120l,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(queueSize),//任务队列
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
    }

    //将客户端socket封装成task，投递到这里
    public void execute(Runnable task){
        this.executor.execute(task);
    }

    public void close() {
        this.executor.shutdown();
    }
}
